function getValue(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

const goodsId = getValue("goodId");
const evaluateUl = document.querySelector(".evaluateCont ul");

//渲染业务
async function render(scoreType, goodsId) {
  let { data: res } = await axios.get(
    "http://cba.itlike.com/public/index.php?s=/api/comment/list",
    {
      params: {
        scoreType,
        goodsId,
        page: 1,
      },
    }
  );
  let evaluateArr = res.data.list.data;
  document.querySelector(".allI").innerHTML = "(" + evaluateArr.length + ")";
  evaluateUl.innerHTML = evaluateArr
    .map((item) => {
      const {
        content,
        create_time,
        is_picture,
        user: { nick_name, avatar_url },
        score,
        images,
      } = item;
      let avatarUrl = avatar_url
        ? avatar_url
        : "http://cba.itlike.com/public/mweb/static/default-avatar.png";


      let str = "";
      for (let i = 0; i < score / 2; i++) {
        str += `
          <i class="iconfont icon-shoucang"></i>
        `;
      }
      let imgVal = [];
      if (!is_picture) {
        imgVal = images
          .map((item) => {
            return `
          <li>
            <img src="${item}">
          </li>
        `;
          })
          .join("");
      } else {
        imgVal = [];
      }

      return `
        <li>
          <div class="evaluteTop">
            <img
              src="${avatarUrl}"
              draggable="false"
            />
            <span>独孤求败</span>
            <div class="star">${str}</div>
            <p class="time">${create_time}</p>
          </div>
          <div class="evaluteDetail">
            <p class="txt">${content}</p>
            <div>
              <ul>${imgVal}</ul>
            </div>
          </div>
        </li>
      `;
    })
    .join("");
}
render(-1, goodsId);
